package org.radar;

import org.apache.commons.cli.*;
import org.radar.core.Manager;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * 命令帮助类
 *
 * @author zhangcs
 */
public class CmdHelper {

    /**
     * 参数 Option
     */
    private static final Options OPTIONS = new Options();

    /**
     * 帮助信息
     */
    private static String HELP_STRING;

    public static enum TYPE {
        EXEC,
        HELP,
        LIST,
        FAILED
    }

    /**
     * 初始化参数信息
     *
     * @param args 参数
     * @return 返回是否初始化成功，成功时返回true
     */
    public static boolean init(String[] args) {
        // 命令解析器
        CommandLineParser parser = new DefaultParser();
        OPTIONS.addOption("H", "help", false, "使用帮助");
        OPTIONS.addOption(Option.builder("P").hasArg(true).longOpt("port").type(Short.TYPE).desc("服务端口（默认: 8888）").build());
        OPTIONS.addOption(Option.builder("I").hasArg(true).longOpt("pid").type(Long.TYPE).desc("目标Java进程的进程号").build());
        OPTIONS.addOption(Option.builder("L").hasArg(true).longOpt("list").type(Long.TYPE).desc("获取Java进程列表").build());
        OPTIONS.addOption(Option.builder("JMX").hasArg(true).longOpt("jmx").desc("开启JMX").build());
        try {
            CommandLine commandLine = parser.parse(OPTIONS, args);

            boolean help = commandLine.hasOption("H");
            if (help) {
                // 打印帮助信息
                System.out.println(getHelp());
                return false;
            }

            boolean isList = commandLine.hasOption("L");
            if (isList) {
                // 打印进程列表
                System.out.println(getHelp());
                return false;
            }

            boolean hasProcessId = commandLine.hasOption("I");
            if (!hasProcessId) {
                System.out.println("请指定目标进程ID" + "\n" + getHelp());
                return false;
            }

            // 目标进程号
            String pId = commandLine.getOptionValue("I");
            Manager.PID = pId;

            // 端口
            String port = commandLine.getOptionValue("P", "8888");
            Manager.PORT = Integer.parseInt(port);

            return true;
        } catch (ParseException e) {
            System.out.println(e.getMessage() + "\n" + getHelp());
        }
        return false;
    }

    /**
     * 获取帮助信息
     *
     * @return 返回帮助信息字符串
     */
    private static String getHelp() {
        if (HELP_STRING != null) {
            return HELP_STRING;
        }

        // 帮助信息
        HelpFormatter helpFormatter = new HelpFormatter();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintWriter printWriter = new PrintWriter(out);
        helpFormatter.printHelp(printWriter,
                HelpFormatter.DEFAULT_WIDTH,
                "java -jar ./radar.jar -I 进程ID",
                null,
                OPTIONS,
                HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD,
                null);
        printWriter.flush();
        HELP_STRING = out.toString();
        printWriter.close();

        return HELP_STRING;
    }

}
